/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "plugin_render.h"
#include "common/log_common.h"
#include "manager/plugin_manager.h"
#include <bits/alltypes.h>
#include <hitrace/trace.h>
#include <native_buffer/native_buffer.h>
#include <native_drawing/drawing_font.h>
#include <native_drawing/drawing_font_collection.h>
#include <native_drawing/drawing_rect.h>
#include <native_drawing/drawing_text_blob.h>
#include <native_drawing/drawing_text_typography.h>
#include <native_drawing/drawing_types.h>

static std::unordered_map<OH_NativeXComponent *, PluginRender *> g_instance;

class MyPoint {
public:
    double x_;
    double y_;

    MyPoint(float x, float y) : x_(x), y_(y) {}

    void setData(float x, float y) {
        x_ = x;
        y_ = y;
    }
};

MyPoint pointA(-1, -1);
MyPoint pointB(0, 0);
MyPoint pointC(0, 0);
MyPoint pointD(0, 0);
MyPoint pointE(0, 0);
MyPoint pointF(0, 0);
MyPoint pointG(0, 0);
MyPoint pointH(0, 0);
MyPoint pointI(0, 0);
MyPoint pointJ(0, 0);
MyPoint pointK(0, 0);

int32_t viewWidth = 0;
int32_t viewHeight = 0;

static void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) {
    DRAWING_LOGI("OnSurfaceCreatedCB");
    if ((component == nullptr) || (window == nullptr)) {
        DRAWING_LOGE("OnSurfaceCreatedCB: component or window is null");
        return;
    }

    auto render = PluginRender::GetInstance(component);
    if (render == nullptr) {
        DRAWING_LOGE("OnSurfaceCreatedCB: render is null");
    }
    OHNativeWindow *nativeWindow = static_cast<OHNativeWindow *>(window);
    render->SetNativeWindow(nativeWindow);

    uint64_t width;
    uint64_t height;
    int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);
    if (xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
        viewHeight = height;
        viewWidth = width;
        DRAWING_LOGI("xComponent width = %{public}lu, height = %{public}lu", width, height);
    }
}

static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) {
    DRAWING_LOGI("OnSurfaceDestroyedCB");
    if ((component == nullptr) || (window == nullptr)) {
        DRAWING_LOGE("OnSurfaceDestroyedCB: component or window is null");
        return;
    }
    PluginRender::Release(component);
}

void PluginRender::SetNativeWindow(OHNativeWindow *nativeWindow) { nativeWindow_ = nativeWindow; }

void PluginRender::Prepare() {
    if (nativeWindow_ == nullptr) {
        DRAWING_LOGE("nativeWindow_ is nullptr");
        return;
    }
    // 这里的nativeWindow是从上一步骤中的回调函数OnSurfaceCreatedCB中获得的
    int32_t usage = NATIVEBUFFER_USAGE_CPU_READ | NATIVEBUFFER_USAGE_CPU_WRITE | NATIVEBUFFER_USAGE_MEM_DMA;
    int ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, SET_USAGE, usage);
    if (ret != 0) {
        DRAWING_LOGE("failed to OH_NativeWindow_NativeWindowHandleOpt");
        return;
    }
    // 通过 OH_NativeWindow_NativeWindowRequestBuffer 获取 OHNativeWindowBuffer 实例
    ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd_);
    DRAWING_LOGI("request buffer ret = %{public}d", ret);

    // 通过 OH_NativeWindow_GetBufferHandleFromNative 获取 buffer 的 handle
    bufferHandle_ = OH_NativeWindow_GetBufferHandleFromNative(buffer_);
    cRect_ = OH_Drawing_RectCreate(0, 0, viewWidth, viewHeight);
    // 使用系统mmap接口拿到bufferHandle的内存虚拟地址
    mappedAddr_ = static_cast<uint32_t *>(
        mmap(bufferHandle_->virAddr, bufferHandle_->size, PROT_READ | PROT_WRITE, MAP_SHARED, bufferHandle_->fd, 0));
    if (mappedAddr_ == MAP_FAILED) {
        DRAWING_LOGE("mmap failed");
    }
}

//void PluginRender::DisPlay() {
//    // 画完后获取像素地址，地址指向的内存包含画布画的像素数据
//    void *bitmapAddr = OH_Drawing_BitmapGetPixels(cBitmap_);
//    uint32_t *value = static_cast<uint32_t *>(bitmapAddr);
//
//    uint32_t *pixel = static_cast<uint32_t *>(mappedAddr_); // 使用mmap获取到的地址来访问内存
//    if (pixel == nullptr) {
//        DRAWING_LOGE("pixel is null");
//        return;
//    }
//    if (value == nullptr) {
//        DRAWING_LOGE("value is null");
//        return;
//    }
//            
//    OH_HiTrace_StartTrace("hitraceTest DisPlay for");
//    // 使用mmap获取到的地址来访问内存
//    uint32_t width = static_cast<uint32_t>(bufferHandle_->stride / 4);
//    for (uint32_t x = 0; x < width; x++) {
//        for (uint32_t y = 0; y < viewHeight; y++) {
//            *pixel++ = *value++;
//        }
//    }
//    OH_HiTrace_FinishTrace();
//    // 设置刷新区域，如果Region中的Rect为nullptr,或者rectNumber为0，则认为OHNativeWindowBuffer全部有内容更改。
//    Region region{nullptr, 0};
//    // 通过OH_NativeWindow_NativeWindowFlushBuffer 提交给消费者使用，例如：显示在屏幕上。
//    OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd_, region);
//    // 内存使用完记得去掉内存映射
//    int result = munmap(mappedAddr_, bufferHandle_->size);
//    if (result == -1) {
//        DRAWING_LOGE("munmap failed!");
//    }
//}

void PluginRender::DisPlay() {
    void *bitmapAddr = OH_Drawing_BitmapGetPixels(cBitmap_);
    uint32_t *value = static_cast<uint32_t *>(bitmapAddr);
    uint32_t *pixel = static_cast<uint32_t *>(mappedAddr_);

    if (pixel == nullptr || value == nullptr) {
        DRAWING_LOGE("pixel or value is null");
        return;
    }

    OH_HiTrace_StartTrace("hitraceTest DisPlay for");
    
    uint32_t width = static_cast<uint32_t>(bufferHandle_->stride / 4);
    size_t totalPixels = width * viewHeight;
    size_t requiredSize = totalPixels * sizeof(uint32_t);
    
    if (requiredSize > bufferHandle_->size) {
        DRAWING_LOGE("Buffer size too small for the view dimensions");
        OH_HiTrace_FinishTrace();
        return;
    }

    // 使用memcpy批量复制
    memcpy(pixel, value, requiredSize);

    OH_HiTrace_FinishTrace();
    
    Region region{nullptr, 0};
    OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd_, region);
    
    if (munmap(mappedAddr_, bufferHandle_->size) == -1) {
        DRAWING_LOGE("munmap failed! Error: %s", strerror(errno));
    }
}

void PluginRender::Create() {
    uint32_t width = static_cast<uint32_t>(bufferHandle_->stride / 4);
    // 创建一个bitmap对象
    cBitmap_ = OH_Drawing_BitmapCreate();
    // 定义bitmap的像素格式
    OH_Drawing_BitmapFormat cFormat{COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
    // 构造对应格式的bitmap
    OH_Drawing_BitmapBuild(cBitmap_, width, viewHeight, &cFormat);

    // 创建一个canvas对象
    cCanvas_ = OH_Drawing_CanvasCreate();
    // 将画布与bitmap绑定，画布画的内容会输出到绑定的bitmap内存中
    OH_Drawing_CanvasBind(cCanvas_, cBitmap_);
    // 使用白色清除画布内容
    OH_Drawing_CanvasClear(cCanvas_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
}

void PluginRender::ConstructPathDefault() {
    cPathA_ = OH_Drawing_PathCreate();
    // 用直线连接到目标点
    OH_Drawing_PathLineTo(cPathA_, 0, viewHeight);
    OH_Drawing_PathLineTo(cPathA_, viewWidth, viewHeight);
    OH_Drawing_PathLineTo(cPathA_, viewWidth, 0);
    // 闭合形状，path绘制完毕
    OH_Drawing_PathClose(cPathA_);
}

void PluginRender::ConstructPathAFromTop() {
    cPathA_ = OH_Drawing_PathCreate();
    // 用直线连接到目标点
    OH_Drawing_PathLineTo(cPathA_, pointC.x_, pointC.y_);
    OH_Drawing_PathQuadTo(cPathA_, pointE.x_, pointE.y_, pointB.x_, pointB.y_);
    OH_Drawing_PathLineTo(cPathA_, pointA.x_, pointA.y_);
    OH_Drawing_PathLineTo(cPathA_, pointK.x_, pointK.y_);
    OH_Drawing_PathQuadTo(cPathA_, pointH.x_, pointH.y_, pointJ.x_, pointJ.y_);
    OH_Drawing_PathLineTo(cPathA_, viewWidth, viewHeight);
    OH_Drawing_PathLineTo(cPathA_, 0, viewHeight);
    // 闭合形状，path绘制完毕
    OH_Drawing_PathClose(cPathA_);
}
void PluginRender::ConstructPathAFromBottom() {
    cPathA_ = OH_Drawing_PathCreate();
    // 指定path的起始位置
    OH_Drawing_PathLineTo(cPathA_, 0, viewHeight);
    // 用直线连接到目标点
    OH_Drawing_PathLineTo(cPathA_, pointC.x_, pointC.y_);
    OH_Drawing_PathQuadTo(cPathA_, pointE.x_, pointE.y_, pointB.x_, pointB.y_);
    OH_Drawing_PathLineTo(cPathA_, pointA.x_, pointA.y_);
    OH_Drawing_PathLineTo(cPathA_, pointK.x_, pointK.y_);
    OH_Drawing_PathQuadTo(cPathA_, pointH.x_, pointH.y_, pointJ.x_, pointJ.y_);
    OH_Drawing_PathLineTo(cPathA_, viewWidth, 0);
    // 闭合形状，path绘制完毕
    OH_Drawing_PathClose(cPathA_);
}

void PluginRender::ConstructPathB() {
    // 下一页的内容绘制
    cPathB_ = OH_Drawing_PathCreate();
    // 指定path的起始位置
    OH_Drawing_PathLineTo(cPathB_, 0, viewHeight);
    // 用直线连接到目标点
    OH_Drawing_PathLineTo(cPathB_, viewWidth, viewHeight);
    OH_Drawing_PathLineTo(cPathB_, viewWidth, 0);
    // 闭合形状，path绘制完毕
    OH_Drawing_PathClose(cPathB_);
}

void PluginRender::ConstructPathC() {
    // 创建一个path对象，然后使用接口连接成一个五角星形状
    cPathC_ = OH_Drawing_PathCreate();
    // 指定path的起始位置
    OH_Drawing_PathMoveTo(cPathC_, pointI.x_, pointI.y_);
    // 用直线连接到目标点
    OH_Drawing_PathLineTo(cPathC_, pointD.x_, pointD.y_);
    OH_Drawing_PathLineTo(cPathC_, pointB.x_, pointB.y_);
    OH_Drawing_PathLineTo(cPathC_, pointA.x_, pointA.y_);
    OH_Drawing_PathLineTo(cPathC_, pointK.x_, pointK.y_);
    // 闭合形状，path绘制完毕
    OH_Drawing_PathClose(cPathC_);
}

void PluginRender::SetPenAndBrush() {
    // 创建一个画刷Brush对象，Brush对象用于形状的填充，执行DrawPath时自动填充
    cBrushA_ = OH_Drawing_BrushCreate();
    OH_Drawing_BrushSetColor(cBrushA_, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xEB, 0xC3));

    cBrushB_ = OH_Drawing_BrushCreate();
    OH_Drawing_BrushSetColor(cBrushB_, OH_Drawing_ColorSetArgb(0xFF, 0xE2, 0xC3, 0x85));

    cBrushC_ = OH_Drawing_BrushCreate();
    OH_Drawing_BrushSetColor(cBrushC_, OH_Drawing_ColorSetArgb(0xFF, 0xBA, 0xAC, 0x91));

    // 将Brush画刷设置到canvas中
    OH_Drawing_CanvasAttachBrush(cCanvas_, cBrushA_);
}

void PluginRender::DrawPath(OH_Drawing_Path *path) {
    // 在画布上画path的形状，五角星的边框样式为pen设置，颜色填充为Brush设置
    OH_Drawing_CanvasDrawPath(cCanvas_, path);
}

napi_value PluginRender::NapiDrawPage(napi_env env, napi_callback_info info) {
    DRAWING_LOGI("NapiDrawPage");
    if ((env == nullptr) || (info == nullptr)) {
        DRAWING_LOGE("NapiDrawPattern: env or info is null");
        return nullptr;
    }

    size_t argc = 1;
    napi_value args[1] = {nullptr};

    napi_value thisArg;
    if (napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr) != napi_ok) {
        DRAWING_LOGE("NapiDrawPattern: napi_get_cb_info fail");
        return nullptr;
    }

    napi_value flipType = 0;
    int32_t flipTypeNum = 0;
    if (napi_ok != napi_get_named_property(env, args[0], "flipType", &flipType)) {
        DRAWING_LOGE("NapiDrawPattern: napi_get_named_property flipType fail");
    }
    if (napi_ok != napi_get_value_int32(env, flipType, &flipTypeNum)) {
        DRAWING_LOGE("NapiDrawPattern: napi_get_value_int32 flipType fail");
    }
    DRAWING_LOGI("NapiDrawPage： flipType=%{public}d", flipTypeNum);

    napi_value positionX = 0;
    int32_t positionXNum = 0;
    napi_get_named_property(env, args[0], "positionX", &positionX);
    napi_get_value_int32(env, positionX, &positionXNum);
    DRAWING_LOGI("NapiDrawPage： positionX=%{public}d", positionXNum);

    napi_value positionY = 0;
    int32_t positionYNum = 0;
    napi_get_named_property(env, args[0], "positionY", &positionY);
    napi_get_value_int32(env, positionY, &positionYNum);
    DRAWING_LOGI("NapiDrawPage： positionYNum=%{public}d", positionYNum);

    napi_value drawPosition = 0;
    int32_t drawPositionNum = 0;
    napi_get_named_property(env, args[0], "drawPosition", &drawPosition);
    napi_get_value_int32(env, drawPosition, &drawPositionNum);
    DRAWING_LOGI("NapiDrawPage： drawPositionNum=%{public}d", drawPositionNum);

    napi_value drawState = 0;
    int32_t drawStateNum = 0;
    napi_get_named_property(env, args[0], "drawState", &drawState);
    napi_get_value_int32(env, drawState, &drawStateNum);
    DRAWING_LOGI("NapiDrawPage： drawStateNum=%{public}d", drawStateNum);

    napi_value exportInstance;
    if (napi_get_named_property(env, thisArg, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
        DRAWING_LOGE("NapiDrawPattern: napi_get_named_property fail");
        return nullptr;
    }


    OH_NativeXComponent *nativeXComponent = nullptr;
    if (napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)) != napi_ok) {
        DRAWING_LOGE("NapiDrawPattern: napi_unwrap fail");
        return nullptr;
    }

    PluginRender *render = PluginRender().GetInstance(nativeXComponent);
    if (render != nullptr) {
        OH_HiTrace_StartTrace("hitraceTest InitData");
        render->InitData(flipTypeNum, positionXNum, positionYNum, drawPositionNum, drawStateNum);
        OH_HiTrace_FinishTrace();
        OH_HiTrace_StartTrace("hitraceTest EmulateInit");
        render->EmulateInit();
        OH_HiTrace_FinishTrace();
        OH_HiTrace_StartTrace("hitraceTest Prepare");
        render->Prepare();
        OH_HiTrace_FinishTrace();
        OH_HiTrace_StartTrace("hitraceTest Create");
        render->Create();
        OH_HiTrace_FinishTrace();
        OH_HiTrace_StartTrace("hitraceTest SetPenAndBrush");
        render->SetPenAndBrush();
        OH_HiTrace_FinishTrace();
        OH_HiTrace_StartTrace("hitraceTest DrawPath");
        if (pointA.x_ == -1 && pointA.y_ == -1) {
            // 无触点时，绘制初始页面
            render->ConstructPathDefault();
            OH_Drawing_CanvasAttachBrush(render->cCanvas_, render->cBrushA_);
            render->DrawPath(render->cPathA_);
        } else {
            // 绘制下一页内容
            OH_Drawing_CanvasAttachBrush(render->cCanvas_, render->cBrushB_);
            render->ConstructPathB();
            render->DrawPath(render->cPathB_);

            // 绘制翻页背面
            OH_Drawing_CanvasAttachBrush(render->cCanvas_, render->cBrushC_);
            render->ConstructPathC();
            render->DrawPath(render->cPathC_);

            OH_Drawing_CanvasAttachBrush(render->cCanvas_, render->cBrushA_);
            // 绘制当前页剩余部分
            if (pointF.x_ == viewWidth && pointF.y_ == 0) {
                // 从右上角开始绘制
                render->ConstructPathAFromTop();
            }
            if (pointF.x_ == viewWidth && pointF.y_ == viewHeight) {
                render->ConstructPathAFromBottom();
            }
            render->DrawPath(render->cPathA_);
            OH_Drawing_CanvasClipRect(render->cCanvas_, render->cRect_, DIFFERENCE, true);
        }
        OH_HiTrace_FinishTrace();
        OH_HiTrace_StartTrace("hitraceTest DisPlay");
        render->DisPlay();
        OH_HiTrace_FinishTrace();
        OH_HiTrace_StartTrace("hitraceTest Destroy");
        render->Destroy();
        OH_HiTrace_FinishTrace();
        DRAWING_LOGI("DrawText executed");
    } else {
        DRAWING_LOGE("render is nullptr");
    }
    return nullptr;
}

PluginRender::~PluginRender() {
    Destroy();

    buffer_ = nullptr;
    bufferHandle_ = nullptr;
    nativeWindow_ = nullptr;
    mappedAddr_ = nullptr;
}

void PluginRender::Destroy() {
    // 销毁创建的对象
    OH_Drawing_BrushDestroy(cBrushA_);
    cBrushA_ = nullptr;
    OH_Drawing_BrushDestroy(cBrushB_);
    cBrushB_ = nullptr;
    OH_Drawing_BrushDestroy(cBrushC_);
    cBrushC_ = nullptr;
    OH_Drawing_PenDestroy(cPenA_);
    cPenA_ = nullptr;
    OH_Drawing_PenDestroy(cPenB_);
    cPenB_ = nullptr;
    OH_Drawing_PenDestroy(cPenC_);
    cPenC_ = nullptr;
    OH_Drawing_PathDestroy(cPathA_);
    cPathA_ = nullptr;
    OH_Drawing_PathDestroy(cPathB_);
    cPathB_ = nullptr;
    OH_Drawing_PathDestroy(cPathC_);
    cPathC_ = nullptr;
    OH_Drawing_RectDestroy(cRect_);

    // 销毁canvas对象
    OH_Drawing_CanvasDestroy(cCanvas_);
    cCanvas_ = nullptr;
    // 销毁bitmap对象
    OH_Drawing_BitmapDestroy(cBitmap_);
    cBitmap_ = nullptr;
}

void PluginRender::Release(OH_NativeXComponent *component) {
    PluginRender *render = PluginRender::GetInstance(component);
    if (render != nullptr) {
        delete render;
        render = nullptr;
        g_instance.erase(g_instance.find(component));
    }
}

void PluginRender::Export(napi_env env, napi_value exports) {
    if ((env == nullptr) || (exports == nullptr)) {
        DRAWING_LOGE("Export: env or exports is null");
        return;
    }
    napi_property_descriptor desc[] = {
        {"drawPage", nullptr, PluginRender::NapiDrawPage, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    if (napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc) != napi_ok) {
        DRAWING_LOGE("Export: napi_define_properties failed");
    }
}

void PluginRender::RegisterCallback(OH_NativeXComponent *nativeXComponent) {
    DRAWING_LOGI("register callback");
    renderCallback_.OnSurfaceCreated = OnSurfaceCreatedCB;
    renderCallback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
    renderCallback_.OnSurfaceChanged = nullptr;
    OH_NativeXComponent_RegisterCallback(nativeXComponent, &renderCallback_);
}

PluginRender *PluginRender::GetInstance(OH_NativeXComponent *component) {
    if (g_instance.find(component) == g_instance.end()) {
        PluginRender *instance = new PluginRender();
        g_instance[component] = instance;
        return instance;
    } else {
        return g_instance[component];
    }
}

void PluginRender::InitData(int flipType, int positionX, int positionY, int drawPosition, int drawState) {
    flipType_ = flipType;
    positionX_ = positionX;
    positionY_ = positionY;
    drawPosition_ = drawPosition;
    drawState_ = drawState;
}

void calcPointABByTouchPoint(MyPoint touchPoint) {
    int r = viewWidth * 6 / 10;
    pointA.x_ = touchPoint.x_;

    // 限制A点的Y值在 半径为宽度*0.6的半圆内
    if (pointF.y_ == viewHeight) {
        // 触摸点在底部，限制触摸点(右下页脚高度)
        int tmpY = viewHeight - abs(sqrt(pow(r, 2) - pow(touchPoint.x_ - (viewWidth - r), 2)));
        pointA.y_ = touchPoint.y_ >= tmpY ? touchPoint.y_ : tmpY;
    } else {
        // 触摸点在上部，限制触摸点（右上页脚高度）
        int tmpY = abs(sqrt(pow(r, 2) - pow(touchPoint.x_ - (viewWidth - r), 2)));
        pointA.y_ = touchPoint.y_ >= tmpY ? tmpY : touchPoint.y_;
    }
}

MyPoint getIntersectionPoint(MyPoint lineOnePointOne, MyPoint lineOnePointTwo, MyPoint lineTwoPointOne,
                             MyPoint lineTwoPointTwo) {
    double x1, y1, x2, y2, x3, y3, x4, y4;
    x1 = lineOnePointOne.x_;
    y1 = lineOnePointOne.y_;
    x2 = lineOnePointTwo.x_;
    y2 = lineOnePointTwo.y_;
    x3 = lineTwoPointOne.x_;
    y3 = lineTwoPointOne.y_;
    x4 = lineTwoPointTwo.x_;
    y4 = lineTwoPointTwo.y_;
    double tmpX = ((x1 - x2) * (x3 * y4 - x4 * y3) - (x3 - x4) * (x1 * y2 - x2 * y1)) /
                  ((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4));
    double tmpY = ((y1 - y2) * (x3 * y4 - x4 * y3) - (x1 * y2 - x2 * y1) * (y3 - y4)) /
                  ((y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4));

    return MyPoint(tmpX, tmpY);
}

void calcPointsXY() {
    pointG.x_ = (pointA.x_ + pointF.x_) / 2;
    pointG.y_ = (pointA.y_ + pointF.y_) / 2;

    pointE.x_ = pointG.x_ - (pointF.y_ - pointG.y_) * (pointF.y_ - pointG.y_) / (pointF.x_ - pointG.x_);
    pointE.y_ = pointF.y_;

    pointH.x_ = pointF.x_;
    pointH.y_ = (pointG.y_ - (pointF.x_ - pointG.x_) * (pointF.x_ - pointG.x_) / (pointF.y_ - pointG.y_));
//    pointH.y_ = pointH.y_ < 0 ? 0 : pointH.y_;

    pointC.x_ = pointE.x_ - (pointF.x_ - pointE.x_) / 2;
    pointC.y_ = pointF.y_;

    pointJ.x_ = pointF.x_;
    pointJ.y_ = pointH.y_ - (pointF.y_ - pointH.y_) / 2;
//    pointJ.y_ = pointJ.y_ < 0 ? 0 : pointJ.y_;

    pointB = getIntersectionPoint(pointA, pointE, pointC, pointJ);
    pointK = getIntersectionPoint(pointA, pointH, pointC, pointJ);

    pointD.x_ = (pointC.x_ + 2 * pointE.x_ + pointB.x_) / 4;
    pointD.y_ = (pointC.y_ + 2 * pointE.y_ + pointB.y_) / 4;

    pointI.x_ = (pointJ.x_ + 2 * pointH.x_ + pointK.x_) / 4;
    pointI.y_ = (pointJ.y_ + 2 * pointH.y_ + pointK.y_) / 4;
}

void PluginRender::EmulateInit() {
    // 获取初始值
    pointA.setData(positionX_, positionY_);
    if (positionX_ == -1 && positionY_ == -1) {
        return;
    }

    MyPoint touchPoint(positionX_, positionY_);
    if (1 == drawPosition_) {
        // 触摸点在上部
        pointF.setData(viewWidth, 0);
        calcPointABByTouchPoint(touchPoint);
    } else if (2 == drawPosition_) {
        // 触摸点在下部
        pointF.setData(viewWidth, viewHeight);
        calcPointABByTouchPoint(touchPoint);
    } else {
        pointA.y_ = viewHeight - 1;
        pointF.setData(viewWidth, viewHeight);
    }

    calcPointsXY();
}